package com.aizuda.boot.modules.business.contract.dto;


import com.aizuda.core.api.ApiErrorCode;
import com.aizuda.core.api.ApiResult;

import java.io.Serializable;

/**
 * 单个对象数据返回
 *
 * @param <T>
 * @author duanyao
 * @date 2022年11月1日
 */
public class SingleResponse<T> extends ApiResult<T> implements Serializable {



    /**
     * 返回失败
     *
     * @param errCode
     * @param errMessage
     * @return
     */
    public static <T> SingleResponse<T> failure(long errCode, String errMessage) {
        SingleResponse<T> response = new SingleResponse<>();
        response.setCode(errCode);
        response.setMessage(errMessage);
        return response;
    }

    /**
     * 返回成功的数据
     *
     * @param <T>
     * @param data
     * @return
     */
    public static <T> SingleResponse<T> of(T data) {
        ApiErrorCode aec = ApiErrorCode.SUCCESS;
        if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
            aec = ApiErrorCode.FAILED;
        }
        SingleResponse<T> tSingleResponse = new SingleResponse<>();
        tSingleResponse.setCode(aec.getCode());
        tSingleResponse.setData(data);
        return tSingleResponse;
    }


    /**
     * 返回结果数据
     *
     * @param <T>
     * @param data
     * @param code
     * @param message
     * @return
     */
    public static <T> SingleResponse<T> of(T data, long code, String message) {
        SingleResponse<T> response = of(data);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    public static SingleResponse success() {
        return of(null);
    }


    public boolean isSuccess() {

        return super.ok();
    }
}

